<?php
	require_once('send_track.conf.php');

	$defaults = array(
			CURLOPT_URL => $config['default']['url'],
			CURLOPT_HEADER => 0,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_TIMEOUT => 4
	);
	$curl_connection = curl_init();
	curl_setopt_array($curl_connection, $defaults);

	if(curl_exec($curl_connection)) {
		if($directory = opendir($config['default']['folder'])) {
			while (false !== ($filename=readdir($directory))){
				if(preg_match("/^queue\.track\..+\.log$/", $filename)) {

					$path = $config['default']['folder'].'/'.$filename;
					$handle = fopen($path, "a");
					$contenLines = file($path);
					$fail = array();

					foreach($contenLines as $line_key=>$line) {
						$line = str_replace(" ", "%20", $line);
						$defaults[CURLOPT_URL] = $config['default']['url']."?tc=".$line;
						curl_setopt_array($curl_connection, $defaults);
						if(!$result = curl_exec($curl_connection)){
							$fail[$line_key] = $line;
							echo 'failure line '.$line_key."\n";
						}
					}
					curl_close($curl_connection);

					if(!empty($fail)) {
						rewind($handle);
						ftruncate($handle, 0);
						fwrite($handle, implode("", $fail));
					} else {
						unlink($path);
					}
				}
			}
			closedir($directory);
		}
	}
?>